﻿using Furion.DependencyInjection;
using MahApps.Metro.Controls;
using MaterialDesignThemes.Wpf;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using UpperComputer.WPF.Entry.ViewModels;
using static System.Collections.Generic.Dictionary<long, string>;

namespace UpperComputer.WPF.Entry
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow, ITransient
    {
        private DoubleAnimation _doubleAnimation;

        private Storyboard _storyboard;

        private DispatcherTimer _timer;

        public static Snackbar Snackbar;

        public MainWindow(MainViewModel mainViewModel)
        {
            InitializeComponent();

            DataContext = mainViewModel;

            _doubleAnimation = new DoubleAnimation();
            _doubleAnimation.From = 0;
            Storyboard.SetTargetName(_doubleAnimation, nameof(LeftShiftTransform));
            Storyboard.SetTargetProperty(_doubleAnimation, new PropertyPath(TranslateTransform.XProperty));
            _storyboard = new Storyboard
            {
                RepeatBehavior = RepeatBehavior.Forever
            };
            _storyboard.Children.Add(_doubleAnimation);
            _timer = new DispatcherTimer(TimeSpan.FromMilliseconds(Constant.TIMER_INTERVAL), DispatcherPriority.Normal, (s, e) =>
            {
                if (_doubleAnimation.To != -LeftShiftList.DesiredSize.Width)
                {
                    _storyboard.Stop(LeftShiftPanel);
                    _doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(((ValueCollection)LeftShiftList.ItemsSource).Count * 3));
                    _doubleAnimation.To = -LeftShiftList.DesiredSize.Width;
                    _storyboard.Begin(LeftShiftPanel, true);
                }
            }, Dispatcher);
            _timer.Start();

            Snackbar = MainSnackbar;
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.OnMouseDownMethod(e);
            MenuToggleButton.IsChecked = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MenuToggleButton.IsChecked = false;
        }

        private void MenuOpen_Click(object sender, RoutedEventArgs e)
        {
            MenuToggleButton.IsChecked = false;
        }

        private void lisView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MenuToggleButton.IsChecked = false;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            MessageBoxResult mbr = this.ShowQuestionBox("确定要退出系统吗");
            if (mbr != MessageBoxResult.OK)
            {
                e.Cancel = true;
                return;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            _timer.Stop();
        }
    }
}